﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace TowerDefense
{
    public class Tower : Entity
    {
        public Tower()
        {
            this.Direction = 0;
            this.Texture = TDGame.textures["tower"];
        }

        public TimeSpan AttackCooldown
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public float Range
        {
            get;
            set;
        }

        public TowerDefense.Munition Munition
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public bool CanAttackGround
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public bool CanAttackAir
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public void AquireTarget()
        {
            var targetData = World.Creeps.Select(c => new { c, range = Library.FindRange(this.Position, c.Position) }).Where(r => r.range < this.Range).OrderBy(r => r.range).FirstOrDefault();
            if (targetData != null)
                CurrentTarget = targetData.c;
            else
                CurrentTarget = null;
        }

        public Creep CurrentTarget
        {
            get;
            set;
        }

        public void Fire()
        {
            throw new System.NotImplementedException();
        }

        public float Direction { get; set; }

        public override void Update(GameTime gameTime)
        {
            if (this.CurrentTarget != null && Library.FindRange(this.Position, CurrentTarget.Position) <= this.Range)
                this.Direction = Library.FindAngle(this.Position, CurrentTarget.Position);
            else
                AquireTarget();
        }

        public enum TowerState
        {
            Firing,
            AcquiringTarget,
            ReadyToRecycle
        }

        public override Entity Clone()
        {
            throw new NotImplementedException();
        }

        public override void Draw(SpriteBatch sb)
        {
            sb.Draw(this.Texture, new Rectangle((int)this.Position.X, (int)this.Position.Y, 20, 20), null, Color.White, this.Direction - (float)Math.PI/4, new Vector2(20, 20), SpriteEffects.None, 0);
        }

        public Texture2D Texture
        {
            get;
            set;
        }

        public Vector2 Position
        {
            get;
            set;
        }
    }
}
